package com.telchina.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.telchina.common.core.domain.AjaxResult;
import com.telchina.common.utils.DateUtils;
import com.telchina.common.domain.WoCategary;
import com.telchina.common.mapper.WoCategaryMapper;
import com.telchina.common.service.IWoCategaryService;
import com.telchina.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 回复单Service业务层处理
 *
 * @author konghao
 * @date 2023-02-21
 */
@Service
public class WoCategaryServiceImpl implements IWoCategaryService {

    @Autowired
    private WoCategaryMapper woCategaryMapper;

    /**
     * 查询回复单
     *
     * @param id 回复单ID
     * @return 回复单
     */
    @Override
    public WoCategary selectById(String id) {
        return woCategaryMapper.selectById(id);
    }

    /**
     * 分页查询回复单列表
     *
     * @param woCategary 回复单
     * @return 回复单
     */
    @Override
    public IPage<WoCategary> selectList(IPage<WoCategary> page, WoCategary woCategary) {
        return woCategaryMapper.selectWoCategaryPage(page, woCategary);
    }

    /**
     * 查询所有回复单列表
     *
     * @param woCategary 回复单
     * @return 回复单
     */
    @Override
    public List<WoCategary> selectListAll(WoCategary woCategary) {
        return woCategaryMapper.selectWoCategaryList(woCategary);
    }

    /**
     * 新增回复单
     *
     * @param woCategary 回复单
     * @return 结果
     */
    @Override
    public int insert(WoCategary woCategary) {
        woCategary.setCreateTime(DateUtils.getNowDate());
        // 自动填充数据
        if (!woCategary.getParentId().equals("0")) {
            WoCategary parentCategary = woCategaryMapper.selectById(woCategary.getParentId());
            woCategary.setSysCode(parentCategary.getSysCode());
            woCategary.setCategaryCode(parentCategary.getCategaryCode());
        }
        return woCategaryMapper.insert(woCategary);
    }

    /**
     * 修改回复单
     *
     * @param woCategary 回复单
     * @return 结果
     */
    @Override
    public int update(WoCategary woCategary) {
        woCategary.setUpdateTime(DateUtils.getNowDate());
        // 自动填充数据
        if (!woCategary.getParentId().equals("0")) {
            WoCategary parentCategary = woCategaryMapper.selectById(woCategary.getParentId());
            woCategary.setSysCode(parentCategary.getSysCode());
            woCategary.setCategaryCode(parentCategary.getCategaryCode());
        }
        return woCategaryMapper.updateById(woCategary);
    }

    /**
     * 批量删除回复单
     *
     * @param ids 需要删除的回复单ID
     * @return 结果
     */
    @Override
    public AjaxResult deleteByIds(String[] ids) {
        // 删除是判断是否包含下级节点
        Integer lastData = woCategaryMapper.selectCount(new QueryWrapper<WoCategary>().in("parent_id", Arrays.asList(ids)));
        if (lastData > 0) {
           return AjaxResult.error("包含子节点，不可删除");
        }else{
            return woCategaryMapper.deleteBatchIds(Arrays.asList(ids)) > 0 ? AjaxResult.success() : AjaxResult.error();
        }
    }

    /**
     * 删除回复单信息
     *
     * @param id 回复单ID
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        return woCategaryMapper.deleteById(id);
    }
}
